using NUnit.Framework;

// ReSharper disable InconsistentNaming
namespace NFreeMarker.Tests.Template
{
    public class TemplateTests_ProcessBuiltInsForNumbers
    {
        [TestFixture]
        public class When_processing_a_c_builtin : TemplateTestsBase
        {
            [Test]
            public void Assure_a_big_number_is_outputted_without_grouping_separators()
            {
                SetTemplateContent("${value?c}");
                AddTemplateVariable("value", 123456789);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("123456789"));
            }

            [Test]
            public void Assure_a_very_small_number_outputted_without_decimals()
            {
                SetTemplateContent("${value?c}");
                AddTemplateVariable("value", 0.00000000000000001m);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("0"));
            }

            [Test]
            public void Assure_a_number_with_more_than_sixteen_decimal_digits_is_trimmed_to_sixteen_decimals()
            {
                SetTemplateContent("${value?c}");
                AddTemplateVariable("value", 0.12345678901234567m);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("0.1234567890123457"));
            }
        }

        [TestFixture]
        public class When_processing_a_ceiling_builtin : TemplateTestsBase
        {
            [Test]
            public void Assure_integers_are_untouched()
            {
                SetTemplateContent("${value?ceiling}");
                AddTemplateVariable("value", 1);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("1"));
            }

            [Test]
            public void Assure_positive_doubles_are_rounded_upwards()
            {
                SetTemplateContent("${value?ceiling}");
                AddTemplateVariable("value", 1.1);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("2"));
            }

            [Test]
            public void Assure_negative_doubles_are_rounded_upwards()
            {
                SetTemplateContent("${value?ceiling}");
                AddTemplateVariable("value", -1.9);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("-1"));
            }
        }

        [TestFixture]
        public class When_processing_a_floor_builtin : TemplateTestsBase
        {
            [Test]
            public void Assure_integers_are_untouched()
            {
                SetTemplateContent("${value?floor}");
                AddTemplateVariable("value", 1);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("1"));
            }

            [Test]
            public void Assure_positive_doubles_are_rounded_downwards()
            {
                SetTemplateContent("${value?floor}");
                AddTemplateVariable("value", 1.9);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("1"));
            }

            [Test]
            public void Assure_negative_doubles_are_rounded_downwards()
            {
                SetTemplateContent("${value?floor}");
                AddTemplateVariable("value", -1.1);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("-2"));
            }
        }

        [TestFixture]
        public class When_processing_a_round_builtin : TemplateTestsBase
        {
            [Test]
            public void Assure_integers_are_untouched()
            {
                SetTemplateContent("${value?round}");
                AddTemplateVariable("value", 1);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("1"));
            }

            [Test]
            public void Assure_positive_doubles_are_rounded_upwards()
            {
                SetTemplateContent("${value?round}");
                AddTemplateVariable("value", 1.5);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("2"));
            }

            [Test]
            public void Assure_positive_doubles_are_rounded_downwards()
            {
                SetTemplateContent("${value?round}");
                AddTemplateVariable("value", 1.4);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("1"));
            }

            [Test]
            public void Assure_negative_doubles_are_rounded_downwards()
            {
                SetTemplateContent("${value?round}");
                AddTemplateVariable("value", -1.5);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("-2"));
            }

            [Test]
            public void Assure_negative_doubles_are_rounded_upwards()
            {
                SetTemplateContent("${value?round}");
                AddTemplateVariable("value", -1.4);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("-1"));
            }
        }

        [TestFixture]
        public class When_processing_a_string_computer_builtin : TemplateTestsBase
        {
            [Test]
            public void Assure_the_value_is_outputted_in_computer_friendly_format()
            {
                SetTemplateContent("${value?string.computer}");
                AddTemplateVariable("value", 1234.5678);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("1234.5678"));
            }
        }

        [TestFixture]
        public class When_processing_a_string_currency_builtin : TemplateTestsBase
        {
            [Test]
            public void Assure_the_value_is_outputted_in_currency_format()
            {
                SetTemplateContent("${value?string.currency}");
                AddTemplateVariable("value", 1234.5678);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("$1,234.57"));
            }
        }

        [TestFixture]
        public class When_processing_a_string_number_builtin : TemplateTestsBase
        {
            [Test]
            public void Assure_the_value_is_outputted_in_number_format()
            {
                SetTemplateContent("${value?string.number}");
                AddTemplateVariable("value", 1234.5678);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("1,234.57"));
            }
        }

        [TestFixture]
        public class When_processing_a_string_percent_builtin : TemplateTestsBase
        {
            [Test]
            public void Assure_the_value_is_outputted_in_percent_format()
            {
                SetTemplateContent("${value?string.percent}");
                AddTemplateVariable("value", 1234.5678);

                string output = ProcessTemplate();

                Assert.That(output, Is.EqualTo("123,456.78 %"));
            }
        }
    }
}
// ReSharper enable InconsistentNaming
